package textapp.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import textapp.business.GroupMessageService;
import textapp.connector.MessageCode;
import textapp.connector.ServerConnectorMessage;
import textapp.dao.model.GroupMessage;

/**
 * Servlet implementation class GetGroupMessageByUserJsonServlet
 */
public class GetGroupMessageByUserJsonServlet extends GenericJsonServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetGroupMessageByUserJsonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	protected Object executeRequestJson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			int userId = Integer.parseInt(request.getParameter("uid"));
			long beginTime = Long.parseLong(request.getParameter("begin"));

			List<GroupMessage> msgs = new GroupMessageService().getGroupMessagesByUserId(userId, beginTime);
			
			return new ServerConnectorMessage(MessageCode.SUCCESS, msgs);
			
		} catch(NumberFormatException ex) {
			return new ServerConnectorMessage(MessageCode.FAIL);
		}
	}



}
